package com.example.moshudriver.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.BeeFramework.Utils.Utils;
import com.BeeFramework.activity.BaseActivity;
import com.BeeFramework.model.BusinessResponse;
import com.BeeFramework.view.ToastView;
import com.sgr.dykdriver.R;
import com.example.moshudriver.model.UserModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.userverifycodeResponse;
import com.example.moshudriver.tools.MessageConstant;
import com.example.moshudriver.utils.MyApplication;
import com.external.androidquery.callback.AjaxStatus;
import com.external.eventbus.EventBus;

public class B1_SignupVerifyActivity extends BaseActivity implements
		BusinessResponse, View.OnClickListener {
	private EditText mMobile;
	private EditText mVerifyCode, b1_edit_pass, b1_edit_passcomfirm;
	private Button mGetVerifyCodeAgain;
	private Button mNext;
	private TextView mLogin;
	private UserModel mUserModel;
	private TimeCount mTime;
	public static String MOBILE = "mobile";
	public static String PASSWORD = "password";
	public static String INVITE = "invite";
	private String invite;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.b1_signup_verify);
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		invite = bundle.getString(B1_SignupVerifyActivity.INVITE);
		mMobile = (EditText) findViewById(R.id.et_mobile);
		mVerifyCode = (EditText) findViewById(R.id.et_verify_code);
		mNext = (Button) findViewById(R.id.btn_next);
		mLogin = (TextView) findViewById(R.id.tv_login);
		mLogin.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);// 下划线
		mGetVerifyCodeAgain = (Button) findViewById(R.id.btn_get_verify_code_again);
		b1_edit_pass = (EditText) findViewById(R.id.b1_edit_pass);
		b1_edit_passcomfirm = (EditText) findViewById(R.id.b1_edit_passcomfirm);
		mNext.setOnClickListener(this);
		mLogin.setOnClickListener(this);
		mGetVerifyCodeAgain.setOnClickListener(this);
		mUserModel = new UserModel(this);
		mUserModel.addResponseListener(this);
		MyApplication.getInstance().addActivity(this);
		EventBus.getDefault().register(this);
	}

	@Override
	public void onClick(View v) {
		String mobile = mMobile.getText().toString();
		String verify_code = mVerifyCode.getText().toString().trim();
		switch (v.getId()) {
		case R.id.tv_login:
			CloseKeyBoard();
			finish();
			break;
		case R.id.btn_next:
			if ("".equals(mobile)) {
				ToastView toast = new ToastView(this,
						getString(R.string.please_input_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mMobile.requestFocus();
			} else if (mobile.length() < 11 || !Utils.isMobile(mobile)) {
				ToastView toast = new ToastView(this,
						getString(R.string.wrong_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mMobile.requestFocus();
			} else if ("".equals(verify_code)) {
				ToastView toast = new ToastView(this,
						getString(R.string.please_input_verify_code));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mVerifyCode.setText("");
				mVerifyCode.requestFocus();
			} else if ("".equals(b1_edit_passcomfirm.getText().toString())
					|| "".equals(b1_edit_pass.getText().toString())) {
				ToastView toast = new ToastView(this,
						getString(R.string.two_passwords_differ_hint));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				b1_edit_passcomfirm.setText("");
				b1_edit_pass.setText("");
				b1_edit_pass.requestFocus();
			} else if (!b1_edit_passcomfirm.getText().toString()
					.equals(b1_edit_pass.getText().toString())) {
				ToastView toast = new ToastView(this,
						getString(R.string.two_passwords_differ_hint));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				b1_edit_passcomfirm.setText("");
				b1_edit_pass.setText("");
				b1_edit_pass.requestFocus();
			} else if (b1_edit_passcomfirm.getText().length() < 6) {
				ToastView toast = new ToastView(this,
						getString(R.string.password_wrong_format_hint));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				b1_edit_passcomfirm.setText("");
				b1_edit_pass.setText("");
				b1_edit_pass.requestFocus();
			} else if (verify_code.length() < 6) {
				ToastView toast = new ToastView(this, "输入6位的短信验证码");
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mVerifyCode.requestFocus();
			}
//			else if ("123456".equals(verify_code)) {
//				Intent intent = new Intent(this, B3_DriverApplyActivity.class);// ��д����ע��
//				 intent.putExtra(MOBILE, mMobile.getText().toString());
//				intent.putExtra(PASSWORD, b1_edit_pass.getText().toString());
//			
//				intent.putExtra(INVITE, invite);
//				startActivity(intent);
//			}
			// //短信验证
			else {
				mUserModel.valid_verifycode(mMobile.getText().toString(),
						verify_code);
			}
			break;
		case R.id.btn_get_verify_code_again:// 发送验证码
			if ("".equals(mobile)) {
				ToastView toast = new ToastView(this,
						getString(R.string.please_input_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mMobile.requestFocus();
			} else if (mobile.length() < 11 || !Utils.isMobile(mobile)) {
				ToastView toast = new ToastView(this,
						getString(R.string.wrong_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mMobile.requestFocus();
			} else {// 这个地方获取短信验证码并且发送

				mUserModel.getVerifyCode(mobile);// 获取短信验证码

				ToastView toast = new ToastView(this, "短信验证码已经发送");
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mVerifyCode.requestFocus();
			}

			break;
		}

	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url.endsWith(ApiInterface.USER_VERIFYCODE)) {
			userverifycodeResponse response = new userverifycodeResponse();
			response.fromJson(jo);
			if (response.succeed == 1) {
				mTime = new TimeCount(60000, 1000);// 构造CountDownTimer对象
				mTime.start();
			} else {
				mMobile.requestFocus();
			}
		} else if (url.endsWith(ApiInterface.USER_VALIDCODE)) {

			Intent intent = new Intent(this, B3_DriverApplyActivity.class);// 注册界面
			intent.putExtra(MOBILE, mMobile.getText().toString());
			intent.putExtra(PASSWORD, b1_edit_pass.getText().toString());
			intent.putExtra(INVITE, invite);
			startActivity(intent);
		}
	}

	// 关闭键盘
	private void CloseKeyBoard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(mMobile.getWindowToken(), 0);
	}

	/* 定义一个倒计时的内部类 */
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			mGetVerifyCodeAgain
					.setText(getString(R.string.get_verify_code_again));
			mGetVerifyCodeAgain.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			mGetVerifyCodeAgain.setClickable(false);
			mGetVerifyCodeAgain.setText(millisUntilFinished / 1000
					+ getString(R.string.resend_after));
		}
	}

	@Override
	protected void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

	public void onEvent(Object event) {
		Message message = (Message) event;
		if (message.what == MessageConstant.SIGN_UP_SUCCESS) {
			finish();
		}
	}
}
